/**
 * Created by Administrator on 2017/9/22.
 */

/**
 * 经纬度从原生获取，原生通过url传递过来
 * */
var weidu = locationSearcher('lat');
var jingdu = locationSearcher('lng');
var startX = 0,
    startY = 0;
if (weidu == null){
    weidu = '';
}
if (jingdu == null){
    jingdu = '';
}
if (C.cityCode == null){
    C.cityCode = '';
}
console.log(C.cityCode);
/**
 * 禁止input输入框输入
 * */
localStorage.setItem('weidu',weidu);
localStorage.setItem('jingdu',jingdu);
// dialog.tusiSuccess(localStorage.getItem('jingdu'),3000);

$('#search-input').attr('readonly', true);
$('#search-input').focus(function () {
    $(this).blur();
});
var bodyTpl = $('#body-template').html();
var bodyCmp = Handlebars.compile(bodyTpl);

var boxTpl = $('#more-template').html();
var boxCmp = Handlebars.compile(boxTpl);

var range = 200, //距下边界长度/单位px
    maxnum = 0, //设置课程总数
    num = 0, //当前数量
    totalheight = 0,
    flag=0,
    start = 1,
    length=10,
    searchName='';
(function () {
    $.ajax({
        url:C.interface.home,
        type:'post',
        dataType:'json',
        data:{
            lat:weidu,
            lng:jingdu,
            cityId:C.cityCode
        },
        success:function (response) {

            if (response.errorCode == '200'){
                var data = response.data;
                $('#body').html(bodyCmp(data));

                var slide = new auiSlide({
                    container:document.getElementById("aui-slide"),
                    // "width":300,
                    "height":150,
                    "speed":500,
                    "autoPlay": 3000, //自动播放
                    "loop":true,
                    "pageShow":true,
                    "pageStyle":'dot',
                    'dotPosition':'center'
                });

                var slide1=new auiSlide({
                    container:document.getElementById("aui-slide1"),
                    // "width":300,
                    "height":228,
                    "speed":500,
                    "autoPlay": 5000, //自动播放
                    "loop":false,
                    "pageShow":true,
                    "pageStyle":'dot',
                    'dotPosition':'center'
                });
                /**
                 * 跳转二级页面
                 * */
                $('.menu-box').bind('click',jumpNewPage);
                lazyImg();
            }else if (response.errorCode == '0'){
                alert('登录过期，请重新登录');
                //调用function.js中的login方法
                againLogin();
            }else {
                alert(response.errorMsg);
            }
        }
    });
})();

//点击搜索之后跳转到搜索页面
/*$('#search-input').focus(function () {
    $('.searchText').show().on('click',function () {
        var val=encodeURI($('#search-input').val());
        console.log(val);
        if(val==''){
            alert('请输入搜索内容');
        }else {
            window.location.href='shopsSearch.html?name='+val;
        }
    });
});*/

$('#search-input').click(searchDate);
$('.aui-searchbar-btn').click(searchDate);

function searchDate() {

    window.location.href = 'shopsSearch.html?version='+ C.version;
    /*var searchData ={
        jump:'jumpSearch'
    };
    var ua = navigator.userAgent.toLowerCase();
    if (/iphone|ipad|ipod/.test(ua)) {
        iosSearch(searchData);
    } else {
        androidSearch(JSON.stringify(searchData));
    }*/
}

//监听滚动高度，加载数据
$(window).on('scroll',function () {
    if (num>maxnum){
        return;
    }
    if ($(document).scrollTop() >= $(document).height() - $(window).height()) {
        pullLoadingMore();
    }
});


/***
 *
 * 上拉加载更多推荐商家
 * */
function pullLoadingMore() {
    if (flag){
        return ;
    }
    flag = 1;
    $('.no-info').hide();
    $('.loading').show();
    $.ajax({
        url:C.interface.homeShopMore,
        type:'post',
        dataType:'json',
        data:{
            start:start,
            length:length,
            lat:C.weidu,
            lng:C.jingdu,
            cityId:C.cityCode
        },
        success:function (response) {
            if (response.errorCode == '200'){
                $('.loading').hide();
                start++;
                var data = response.data;
                maxnum = data['count'];
                if (maxnum == 0){
                    $('.no-info').show();
                }
                if (data.stores.length > 0){
                    $('#shop-box').append(boxCmp(data));

                    new auiLazyload({
                        errorImage:'../img/errorimg.png'
                    });
                    // $('.menu-box').bind('click',jumpNewPage);
                }else {
                    $('.no-info').show();
                    num = maxnum +1;
                }

                flag = 0;

            }else if (response.errorCode == '0'){
                alert('登录过期请重新登录');
                againLogin();
            }else {
                alert(response.errorMsg);
            }
        }
    });
}


/**
 * 二级栏跳转新的页面
 * */
function jumpNewPage(event) {
    event.preventDefault();
    var id = $(this).data('id');
    switch (id){
        case 0:
            location.href = 'category_all.html?categoryId='+id;
            break;
        case '9e14ecefa64f4f3ca552f8e4831b13f0':
        case 'ccd94ab1a52e41729b53a5d4bbeb1fef':
        case 'a78b77072b074f13b98ade5adb1693f7':
        case 'cf5b20e016f74f279c9b3761a253c9ef':
        case '366170b8530f4cdeb97969a1e328929f':
        case '340ec8d64af844c3a8af9ee6aebc20cd':
        case '2b0cfb73689b408f91e1ecc7cc1b4259':
            location.href = 'category.html?categoryId='+id;
            break;
        case 'd679f34bb993474ea55edfe180fc53f9':
            location.href = 'turnplate.html';
            break;
    }
}



function iosSearch(param) {
    window.webkit.messageHandlers.jumpSearch.postMessage(param);
}
function androidSearch(param) {
    window.huifa.jumpSearch(param);
}

/**
 * 开始滑动
 * */
function touchStart(evt){
    try{
        var touch = evt.touches[0], //获取第一个触点
            x = Number(touch.pageX), //页面触点X坐标
            y = Number(touch.pageY); //页面触点Y坐标
        //记录触点初始位置
        startX = x;
        startY = y;
    }catch(e){
        console.log(e.message)
    }
}

function touchMove(evt){
    try{
        var touch = evt.touches[0], //获取第一个触点
            x = Number(touch.pageX), //页面触点X坐标
            y = Number(touch.pageY); //页面触点Y坐标
        //判断滑动方向
        if (y - startY<0) {
            console.log('上滑了！');
            $('#search').css('top','20px');
        }else {
            console.log('下滑了');
            $('#search').css('top','0px');
        }
    }catch(e){
        console.log(e.message);
    }
}
// function touchEnd(evt){
//     try{
//         var touch = evt.touches[0], //获取第一个触点
//                 x = Number(touch.pageX), //页面触点X坐标
//                 y = Number(touch.pageY); //页面触点Y坐标
//         //判断滑动方向
//         if (y - startY<0) {
//             console.log('上滑了！');
//         }
//     }catch(e){
//         console.log(e.message);
//         console.log("end");
//     }
// }
//绑定事件
function bindEvent(){
    document.addEventListener('touchstart',touchStart,false);
    document.addEventListener('touchmove',touchMove,false);
    // document.addEventListener('touchend',touchEnd,false);
}

//bindEvent();